<?php 
namespace Admin\Controller;
use Think\Controller;

class GoodsController extends AdminController
{
	public function index()
	{
		$goods = D('goods');
		$list = $goods->field('goods.gname,goodsimg.gid,goodsimg.icon,goodsimg.face,goods.id,goods.cid,goods.price,goods.stock,goods.cknum,goods.sales,goods.up,goods.hot,goods.des,goods.uptime')->join('goodsimg on goods.id=goodsimg.gid')->where(array('face'=>1))->select();
		// var_dump($list);die;
		$this->assign('list',$list);// 赋值数据集
		$this->display(); // 输出模板
	}

	public function add()
	{
		$category=D('goodscategory');
		$list = $category->getAdminCate();
		$this->assign('list',$list);// 赋值数据集
		$this->display('Goods/add');
	}

	public function doadd()
	{
        $goods=D('Goods');
        $goodsimg=D('Goodsimg');
        // $gid=I('get.id/d');
        if(!empty($_FILES)){
			//上传单个图像
			$upload=new \Think\Upload();
	    	$upload->maxSize=3145678;
	    	$upload->exts=array('jpg','gif','png','jpeg');
	    	$upload->rootPath='Uploads/';
	    	$upload->savePath='';
	    	$upload->saveName='com_create_guid';
	    	$upload->autoSub=true;
	        $upload->subName = array('date','Ymd');
	    	$info=$upload->uploadOne($_FILES['img']);
	    	if (!$info) {
	    		$this->error($upload->getError());
	    	} else {
	    		$filepath = $info['savepath'].$info['savename'];
	    		$data['img']=$filepath;
	    		$data['cid']=I('post.cid');
	    		$data['gname']=I('post.gname');
	    		$data['price']=I('post.price');
	    		$data['stock']=I('post.stock');
	    		$data['cknum']=I('post.cknum');
	    		$data['sales']=I('post.sales');
	    		$data['up']=I('post.up');
	    		$data['hot']=I('post.hot');
	    		$data['des']=I('post.des');
	    		$data['uptime']=time();
	    		// $data['face']=2;
	    		
	    		$goods->create($data);
	    		$result=$goods->add();
		    	    if(!$result){
		    			$this->error('上传失败!');
		    		}else{
		    			// var_dump($data['img']);exit;
		    			$img=$data['img'];
		    			$data1['icon']=$filepath;
		    			$gid=$goods->field('id')->where(array('img'=>$img))->select();
			    		$data1['gid']=$gid[0]['id'];
		    			// var_dump($data1);exit;
			    		$goodsimg->create($data1);
			    		$result=$goodsimg->add();
			    		if(!$result){
			    			$this->error('上传失败!');
			    		}else{
			    			$this->success('上传成功!',U('index'));
			    	    }
	    		    }

	    		

	    	}
		}
	}

	public function edit()
	{
		$category=D('Goodscategory');
		$list1 = $category->getAdminCate();
		// $list1 = $category->select();
		$this->assign('list1',$list1);// 赋值数据集

		$id=I('get.id/d');
        $data=M('Goods')->where(array('id'=>$id))->find(); 
        $this->assign('data',$data);
        $this->display();
	}
  

	public function update()
    {
    	if(empty($_POST)){
    		$this->error('请填写相应数据',U('edit'));
    		exit;
    	}
    	M('Goods')->create();

    	// var_dump(M('Goodscategory')->create());
    	// 	var_dump(M('Goodscategory')->save());exit;
    	if(M('Goods')->save()){
    		$this->success('修改成功',U('index'));
    	}else{
    		// echo M('Goodscategory')->fetchSql(true)->save().'<br>';exit;
    		$this->error('修改失败');
    	}
    }	

    //删除
	public function del()
	{
		//判断有无参数ID
        if (empty($_GET['id'])) {
            $this->redirect('goods/index');
            exit;
        }
        $id = I('get.id/d');
        $img=M('goods')->where(array('id'=>$id))->find();
        // var_dump($img['img']);exit;
        $path="./Uploads/";
        // var_dump($path);
        // $gid=$id;
        $goodsimg_id=M('goodsimg')->field('id')->where(array('gid'=>$id))->select();
        if($goodsimg_id){
        	 $this->error('删除失败,请先清空图片',U('index'));
        }else{
            unlink($path.$img['img']);
	        if (M('goods')->delete($id) > 0) {
	        	$this->success('恭喜您!删除成功!',U('index'));
	        } else {
	            $this->error('删除失败!',U('index'));
	        }
	    }
	}

	public function img()
	{
		$goodsimg=D('goodsimg');
		$gid=I('get.id/d');
		// var_dump($gid);exit;
        $data=$goodsimg->where('gid='.$gid)->select();
        // var_dump($data);exit;
        $this->assign('list',$data); 
        $this->assign('gid',$gid);
		$this->display();
	}


	public function upload()
	{
		$uploadimg=M('goodsimg');
		if(!empty($_FILES)){
			//上传单个图像
			$upload=new \Think\Upload();
	    	$upload->maxSize=3145678;
	    	$upload->exts=array('jpg','gif','png','jpeg');
	    	$upload->rootPath='Uploads/';
	    	$upload->savePath='';
	    	$upload->saveName='com_create_guid';
	    	$upload->autoSub=true;
	        $upload->subName = array('date','Ymd');
	    	$info=$upload->uploadOne($_FILES['icon']);
	    	if (!$info) {
	    		$this->error($upload->getError());
	    	} else {
	    		$filepath = $info['savepath'].$info['savename'];
	    		$data['icon']=$filepath;
	    		$data['gid']=I('post.gid/d');
	    		$data['face']=2;
	    		// var_dump($data['gid']);exit;
	    		$uploadimg->create($data);
	    		$result=$uploadimg->add();
	    		if(!$result){
	    			$this->error('上传失败!');
	    		}else{
	    			$this->success('上传成功!');
	    		}
	    	}
		}
	}	

	public function delimg()
	{
        //判断有无参数ID
        if (empty($_GET['id'])) {
            $this->redirect('index');
            exit;
        }
        $id = I('get.id/d');
        $img=M('goodsimg')->where(array('id'=>$id))->find();
        // var_dump($img);exit;
        $path="./Uploads/";
        // var_dump($path);
        unlink($path.$img['icon']);
        if (M('goodsimg')->delete($id) > 0) {
            $this->success('恭喜您!删除成功!');
        } else {
            $this->error('删除失败!',U('index'));
        }
    }

    public function up()
    {
    	$id = I('get.id/d');
		$data['up'] = $_GET['s'];
		$goods = M('Goods');
		$goods->where(array('id'=>$id))->save($data);
        $this->redirect('index');
    }

    public function hot()
    {
    	$id =  I('get.id/d');
		$data['hot'] = $_GET['s'];
		$goods = M('Goods');
		$goods->where(array('id'=>$id))->save($data);
        $this->redirect('index');
    }

    public function face()
    {
    	$goods = M('goodsimg');
    	if($_GET['s']==1){
            header('location:'.$_SERVER['HTTP_REFERER']);
    	}

    	$id =  I('get.id/d');
    	// var_dump($id);
    	// $lists=[];
    	$gid =  I('get.gid/d');
    	// $list=M('goodsimg')->where(array('id'=>$id))->find();
    	// $face=M('goodsimg')->where(array('face'=>1,'gid'=>$gid))->find();
    	// var_dump($face);exit;


        $data['face']=2;
    	if($goods->where(array('gid'=>$gid))->save($data)){
    		  $data['face']=1;
    		  // $img=M('goodsimg')->where(array('face'=>1,'gid'=>$id))->find();
    		  // $data['img']=$img['icon'];
              $goods->where(array('id'=>$id))->save($data);
              header('location:'.$_SERVER['HTTP_REFERER']);
    	// var_dump($data);exit;
    	}
    	// var_dump($list);
    	// var_dump($face);
    	// $face['face']=2;
    	// $list['face']=1;
    	// $lists[0]=$face;
    	// $lists[1]=$list;

    	// var_dump($lists);die;
    	// var_dump($_GET['s']);
    	// $other = 3-$_GET['s'];

    	// var_dump($lists);
		
		
		// $goods->where(array('id'=>$id))->save($data);
        // $this->success();
    }

    
}